package com.lvtongka.app.ui.splash

import android.content.Intent
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.viewpager.widget.ViewPager
import com.lvtongka.app.R
import com.lvtongka.app.base.BaseActivity
import com.lvtongka.app.databinding.ActivityIntroBinding
import com.lvtongka.app.helper.SPHelper
import com.lvtongka.app.ui.login.LoginRevisionActivity
import com.lvtongka.app.ui.login.LoginViewModel
import com.lvtongka.app.ui.splash.adapter.IntroPageAdapter
import com.lvtongka.app.ui.splash.fragment.IntroFragment1
import com.lvtongka.app.ui.splash.fragment.IntroFragment2
import com.lvtongka.app.ui.splash.fragment.IntroFragment3


class IntroActivity : BaseActivity<ActivityIntroBinding, LoginViewModel>(),
    ViewPager.OnPageChangeListener {
    private var adapter: IntroPageAdapter? = null
    private var realPosition = 0
    private var screenWidth = 0

    private val list = mutableListOf<Fragment>(
        IntroFragment1(), IntroFragment2(), IntroFragment3()
    )

    companion object {
        const val TAG = "IntroActivity_zw_test"
        const val NUM_PAGES = 3
    }

    override fun initBinding(): ActivityIntroBinding {
        return ActivityIntroBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        isBlack = false
        super.onCreate(savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        // 获取屏幕宽度
        val displayMetrics = DisplayMetrics()
        val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
        windowManager.defaultDisplay.getMetrics(displayMetrics)
        screenWidth = displayMetrics.widthPixels

        val params: ViewGroup.LayoutParams = binding.tvLogin.layoutParams
        params.width = (screenWidth * 0.6).toInt()
        binding.tvLogin.setLayoutParams(params)
    }

    override fun initViewModel(): LoginViewModel {
        return ViewModelProvider(this)[LoginViewModel::class.java]
    }

    override fun initEvent() {
        binding.viewPager.addOnPageChangeListener(this)
        binding.tvSkip.setOnClickListener {
            startActivity(Intent(this@IntroActivity, LoginRevisionActivity::class.java))
            finish()
        }
        binding.tvLogin.setOnClickListener {
            startActivity(Intent(this@IntroActivity, LoginRevisionActivity::class.java))
            finish()
        }
    }

    override fun initData() {
        SPHelper.putBoolean("accept_yd", true)
        adapter = IntroPageAdapter(
            supportFragmentManager,
            list
        )
        binding.viewPager.adapter = adapter
        binding.viewPager.setCurrentItem(0, false)
    }


    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

    }

    override fun onPageSelected(position: Int) {
        realPosition = position
        setSelectPoint(position)
    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    private fun setSelectPoint(realPosition: Int) {
        when(realPosition) {
            0->{
                binding.iv1.setBackgroundResource(R.drawable.bg_intro_dot_selected)
                binding.iv2.setBackgroundResource(R.drawable.bg_intro_dot)
                binding.iv3.setBackgroundResource(R.drawable.bg_intro_dot)
                binding.tvLogin.visibility = View.GONE
            }
            1->{
                binding.iv1.setBackgroundResource(R.drawable.bg_intro_dot)
                binding.iv2.setBackgroundResource(R.drawable.bg_intro_dot_selected)
                binding.iv3.setBackgroundResource(R.drawable.bg_intro_dot)
                binding.tvLogin.visibility = View.GONE
            }
            2->{
                binding.iv1.setBackgroundResource(R.drawable.bg_intro_dot)
                binding.iv2.setBackgroundResource(R.drawable.bg_intro_dot)
                binding.iv3.setBackgroundResource(R.drawable.bg_intro_dot_selected)
                binding.iv3.postDelayed({
                    if (this.realPosition == 2) {
                        binding.tvLogin.visibility = View.VISIBLE
                    }
                }, 500)

            }
        }
    }

    override fun onDestroy() {
        binding.viewPager.removeOnPageChangeListener(this)
        super.onDestroy()
    }
}